package com.shsubway.activity.manger;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shsubway.R;
import com.shsubway.adapter.ScoreAdapter;
import com.shsubway.adapter.TestAdapter;
import com.shsubway.base.BaseActivity;
import com.shsubway.model.ScoreEntity;
import com.shsubway.model.StaffEntity;
import com.shsubway.model.TestEntity;
import com.shsubway.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 成绩的查看
 */
public class ScoreActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    Intent intent;
    Bundle bundle;
    StaffEntity staffEntity;
    ScoreAdapter scoreAdapter;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_rec)
    RecyclerView myRec;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    private int limit = 10;        // 每页的数据是10条
    private int curPage = 0;        // 当前页的编号，从0开始
    TestEntity testEntity;
    boolean isManager = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySwf.setOnRefreshListener(() -> addData());
        myRec.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (isManager)
                toast("第"+(position+1)+"名");
            }
        });
    }

    /**
     * 初始化adapter 通过权限去判断
     */
    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        staffEntity = (StaffEntity) bundle.getSerializable(Constant.STAFFENTITY);
        isManager = getIsManager();
        setTitleBar(toolbar,"查看成绩");
        if (isManager) {
            testEntity = (TestEntity) bundle.getSerializable(Constant.TESTENTITY);

        }
        initAdapterManager();
        setRefresh(mySwf);
        addData();
    }

    private boolean getIsManager() {
        if (staffEntity.getStaff_permissions() == 1) {
            return true;
        } else {

            return false;
        }
    }

    private void addData() {


        BmobQuery<ScoreEntity> bmobQuery = new BmobQuery<>();
        //超管查询
        if (isManager) {
            BmobQuery<ScoreEntity> eq1 = new BmobQuery<ScoreEntity>();
            eq1.addWhereEqualTo("testId", testEntity.getObjectId());
            BmobQuery<ScoreEntity> eq2 = new BmobQuery<ScoreEntity>();
            eq2.addWhereEqualTo("testType", 0);
            List<BmobQuery<ScoreEntity>> andQuerys = new ArrayList<BmobQuery<ScoreEntity>>();
            andQuerys.add(eq1);
            andQuerys.add(eq2);
            bmobQuery.and(andQuerys);
            bmobQuery.order("-score");
        } else {
            bmobQuery.addWhereEqualTo("userId", staffEntity.getObjectId());
            bmobQuery.order("-createdAt");
        }
        bmobQuery.setLimit(limit);
        if (curPage != 0) {
            bmobQuery.setSkip(curPage * limit);
        } else {
            bmobQuery.setSkip(curPage);
        }
        bmobQuery.findObjects(new FindListener<ScoreEntity>() {
            @Override
            public void done(List<ScoreEntity> list, BmobException e) {
                mySwf.setRefreshing(false);
                if (e == null) {
                    Log.e("查询成功：共", list.size() + "条数据。");
                    if (curPage == 0) {
                        scoreAdapter.setNewData(list);
                    } else {
                        scoreAdapter.addData(list);
                    }
                    if (list.size() != 10) {
                        scoreAdapter.loadMoreEnd(false);
                    } else {
                        scoreAdapter.loadMoreComplete();
                        curPage++;
                    }

                } else {
                    toast("获取失败，请稍后重试！");
                }
            }
        });
    }

    private void initAdapterManager() {
        setRecy(myRec);
        scoreAdapter = new ScoreAdapter(R.layout.item_score_deatil, new ArrayList<>(), isManager);
        scoreAdapter.setEmptyView(emptyView);
        scoreAdapter.setOnLoadMoreListener(this);
        scoreAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        myRec.setAdapter(scoreAdapter);

    }


    @Override
    public void onLoadMoreRequested() {
        addData();
    }
}
